/**
 * pi.debugger <http://pi-js.googlecode.com>
 * v1.0a
 * 27.03.2008, 5:44 AM ~ 1.04.2007 21:32
 * Azer Koçulu <http://azer.kodfabrik.com>
 */

if(pi.env.ie){
	function XPathResult(){};
}

pi.dbgr = {
	env:{ "init":false, "ctmp":[], "dIndex":0, "dNames":["console","html","css","dom","about"], "consoleType":1, "multilinemode":false, "activeNavigation":null },
	init:function(){
		pi.dbgr.el = new pi.dict; // elements
		pi.dbgr.el.content = new pi.dict;
		with(pi.dbgr){
			
			document.documentElement.childNodes[0].appendChild(
				new pi.element("link").attribute.set("rel","stylesheet").attribute.set("href","debugger.css").environment.getElement()
			);

			/* 
			 * form
			 */
			el.main = new pi.element("DIV").attribute.set("id","PIDebugger").insert(document.body);
			
			el.buttonset = new pi.element().attribute.addClass("ButtonSet").insert(el.main);
			el.content.console = new pi.element("DIV").attribute.addClass("Content").insert(el.main);
			el.content.html = new pi.element("DIV").attribute.addClass("Content").environment.addStyle({ "display":"none" }).insert(el.main);
			el.content.css = new pi.element("DIV").attribute.addClass("Content").environment.addStyle({ "display":"none" }).insert(el.main);
			el.content.dom = new pi.element("DIV").attribute.addClass("Content").environment.addStyle({ "display":"none" }).insert(el.main);
			el.content.about = new pi.element("DIV").attribute.addClass("Content About").environment.addStyle({ "display":"none" }).update("<div><h3>&#960; Debugger</h3><strong>Version:</strong> V1.0a<br /><strong>Visit Homepage:</strong> <a href='http://pi-js.googlecode.com'>http://pi-js.googlecode.com</a><br /><strong>Developer:</strong> Azer Ko&#231;ulu &#60;<a href='http://azer.kodfabrik.com'>http://azer.kodfabrik.com</a>&#62;</div>").insert(el.main);
			
			// buttons
			if(pi.env.opera)el.buttonset.environment.addStyle({ "width":"99%" });
			el.aboutTabButton = new pi.element("A").environment.addStyle({ "font":"12px Georgia,Times,'Times New Roman'" }).update("&#960").event.addListener("click",d.navigate.curry(pi.dbgr,4)).insert( new pi.element("DIV").attribute.addClass("Button").attribute.set("title","PI.Debugger v1.a").insert(el.buttonset) );
			el.consoleTabButton = new pi.element("A").update("Console").insert( new pi.element("DIV").attribute.addClass("Button Selected").event.addListener("click",d.navigate.curry(pi.dbgr,0)).insert(el.buttonset) );
			el.htmlTabButton = new pi.element("A").update("HTML").insert( new pi.element("DIV").attribute.addClass("Button").event.addListener("click",d.navigate.curry(pi.dbgr,1)).insert(el.buttonset) );
			el.cssTabButton = new pi.element("A").update("CSS").insert( new pi.element("DIV").attribute.addClass("Button").event.addListener("click",d.navigate.curry(pi.dbgr,2)).insert(el.buttonset) );
			el.domTabButton = new pi.element("A").update("DOM").insert( new pi.element("DIV").attribute.addClass("Button").event.addListener("click",d.navigate.curry(pi.dbgr,3)).insert(el.buttonset) );
			
			el.minimizeButton = new pi.element("A").update("_").event.addListener("click",d.minimize).insert( new pi.element("DIV").attribute.addClass("Button WindowButton MinimizeButton").insert(el.buttonset) );
			el.maximizeButton = new pi.element("A").update("&#94;").event.addListener("click",d.maximize).insert( new pi.element("DIV").attribute.addClass("Button WindowButton MaximizeButton").insert(el.buttonset) );
			
			el.closeButton = new pi.element("A").update("x").event.addListener("click",d.close).insert( new pi.element("DIV").attribute.addClass("Button WindowButton CloseButton").insert(el.buttonset) );
			
			// xpath query
			el.queryInput = new pi.element("INPUT").attribute.addClass("QueryInput").update(pi.env.ie?"Internet Explorer doesn't support HTML XPath":"//div").insert(el.content.console).event.addListener("keyup",listen.queryInput);
			if(pi.env.ie)el.queryInput.attribute.set("disabled","true");
			el.queryTitle = new pi.element("STRONG").attribute.addClass("QueryTitle").child.add(
				document.createTextNode("XPath Query")
			).insert(el.content.console);
			
			// console
			el.consoleFrame = new pi.element("DIV").attribute.addClass("ConsoleFrame Frame").insert(el.content.console);
			el.console = new pi.element("DIV").attribute.addClass("Console").insert(el.consoleFrame);
			el.consoleOutput = new pi.element("DIV").insert(new pi.element("DIV").attribute.addClass("ConsoleOutput").insert(el.console));
			el.console.child.add(new pi.element("STRONG").attribute.addClass("Arrow").update(">>>").insert(el.console));
			el.consoleInput = new pi.element("TEXTAREA").attribute.addClass("ConsoleInput").event.addListener("keydown",listen.consoleInput).insert(el.console);
			el.console.child.add(
				new pi.element("A").attribute.addClass("MlToggleButton").event.addListener("click",d.console.openNavigation).update("&#94;")
			);
			
			el.content.console.child.add(new pi.element().environment.addStyle({ "clear":"both" }));
			
			// html
			el.htmlFrame = new pi.element("DIV").environment.addStyle({ "marginTop":pi.env.ie?"50px":"10px" }).attribute.addClass("HTMLFrame Frame").insert(el.content.html);
			el.htmlTree = new pi.element("DIV").attribute.addClass("Tree").insert(el.htmlFrame);
			el.htmlRightFrame = new pi.element("DIV").attribute.addClass("RightFrame").insert(el.htmlFrame);
			el.htmlRightNav = new pi.element("SELECT").attribute.addClass("Nav").event.addListener("change",listen.htmlNav).insert(el.htmlRightFrame).child.add(
				new pi.element("OPTION").update("Computed Style")
			).child.add(
				new pi.element("OPTION").update("DOM")
			);
			el.htmlRightContent = new pi.element("DIV").attribute.addClass("RightContent").insert(el.htmlRightFrame);
			
			// css
			el.cssStylesheetSelectbox = new pi.element("SELECT").attribute.addClass("StylesheetSelectbox").event.addListener("change",listen.stylesheetSelectbox).insert(el.content.css);
			el.cssFrame = new pi.element("DIV").attribute.addClass("CSSFrame Frame").insert(el.content.css);
			el.cssContent = new pi.element("DIV").attribute.addClass("CSSContent").insert(el.cssFrame);
			
			// dom
			el.content.dom.child.add(new pi.element("LABEL").update("Enter manual path:"));
			el.domInputbox = new pi.element("INPUT").attribute.addClass("DOMInput").update("window").event.addListener("keyup",listen.domInputbox).insert(el.content.dom);
			el.domFrame = new pi.element("DIV").attribute.addClass("DOMFrame Frame").insert(el.content.dom);
			el.domContent = new pi.element("DIV").attribute.addClass("DOMContent").insert(el.domFrame);
			el.domLeftContent = new pi.element("DIV").attribute.addClass("Left").insert(el.domContent);
			el.domRightContent = new pi.element("DIV").attribute.addClass("Right").insert(el.domContent);
			el.domContent.child.add(new pi.element().environment.addStyle({ "clear":"both" }));
			
			/* 
			 * navigation
			 */
			
			el.consoleNavigation = new pi.element("DIV").attribute.addClass("Navigation").child.add(
				new pi.element("A").event.addListener("click",d.console.toggleMultiline).update("Open/Close Multiline")
			).child.add(
				new pi.element("A").event.addListener("click",d.console.clear).environment.addStyle({ "borderBottom":"0" }).update("Clear Console")
			).environment.addStyle({ "top":"225px", "right":"15px" }).insert(el.content.console);
		
			env.init = true;
			
			for(var i=new pi.iterator(env.ctmp); !i.atEnd;){
				var item = i.next();
				d.console.log(item);
			}
			
			d.refreshSize();
		}	
	},
	d: {
		close:function(){
			with(pi.dbgr){
				el.main.clean();
				el.main.remove();
			}
		},
		minimize:function(){
			with(pi.dbgr){
				el.main.environment.addStyle({ "height":"30px" });
				el.minimizeButton.environment.getParent().pi.environment.addStyle({ "display":"none" });
				el.maximizeButton.environment.getParent().pi.environment.addStyle({ "display":"block" });
			}
		},
		maximize:function(){
			with(pi.dbgr){
				el.main.environment.addStyle({ "height":"295px" });
				el.minimizeButton.environment.getParent().pi.environment.addStyle({ "display":"block" });
				el.maximizeButton.environment.getParent().pi.environment.addStyle({ "display":"none" });
			}
		},
		console:{
			dir:function(VALUE){
				with(pi.dbgr){
					d.navigate(3);
					d.dom.open(VALUE);
				}
			},
			log:function(VALUE){
				with (pi.dbgr) {
					if(env.init==false){
						env.ctmp.push(VALUE);
						return;
					}
					d.console.addLine().update(d.console.format(VALUE));
					d.console.scroll();
				}
			},
			addLine:function(){
				with (pi.dbgr) {
					return new pi.element("DIV").attribute.addClass("Row").insert(el.consoleOutput);
				}
			},
			clear:function(){
				with(pi.dbgr){
					el.consoleOutput.clean();
					d.closeActiveNavigation();
				}
			},
			format:function(VALUE,DEPTH){
				with(pi.dbgr){
					var str = "";
					
					if(VALUE instanceof XPathResult){
						if(VALUE.resultType>5){
							var arr = [];
							for(var i=new pi.iterator(VALUE); !i.atEnd;)
								arr.push(i.next());
							return "<span class='Object'>XPath Result snapshotLength<em>="+VALUE.snapshotLength+"</em> resultType<em>="+VALUE.resultType+"</em></span> <br />"+pi.dbgr.d.console.format(arr);
						} else
							return "<span class='Object'>XPath Result resultType<em>="+VALUE.resultType+"</em></span>";
					}
					
					var t = typeof VALUE;
					if(["string","number","boolean"].indexOf(t)>-1){
						str+="<span class='{0}'>{1}{2}{1}</span>".format(
							t,
							t=="string"?"\"":"",
							VALUE
						);
					} 
					else if(VALUE==null){
						str+="<span class='Null'>null</span>";
					}
					else if(t=="object"&&VALUE!=null&&"nodeName" in VALUE){
						str+="<span class='Element'>&#60;<span class='tagName'>"+VALUE.nodeName+"</span> ";
						var at = VALUE.attributes;
						if(at)
							for(var i=0; i<at.length; i++){
								var item = at[i];
								if(pi.env.ie&&Boolean(item.nodeValue)==false)continue;
								str+=" <span class='attributeName'>"+item.nodeName+"</span>=<span class='attributeValue'>\""+item.nodeValue+"\"</span>";
								if(i==2)break;
							}
						str+="&#62;</span>";
					}
					else if(["array","object"].indexOf(t)>-1||(typeof NodeList!="undefined"&&VALUE instanceof NodeList)||(typeof VALUE=="object"&&VALUE!=null&&VALUE.length&&VALUE.item)){
						var obj = VALUE;
						var arr = obj instanceof Array||(typeof obj=="object"&&obj!=null&&obj.length&&obj.item);
						if (DEPTH&&!arr) {
							str+="<span>Object</span>"
						}
						else {
							str += "<span class='{0}'>{1} ".format(arr ? "Array " : "Object ", arr ? "[" : "Object ");
							for (var i = new pi.iterator(obj); !i.atEnd;) {
								var item = i.next();
								str += i.environment.getIndex() > 0 ? ", " : "";
								str += arr ? (" <em>" + d.console.format(item,true) + "</em>") : (item[0] + "=<em>" + d.console.format(item[1], true) + "</em>");
								if(DEPTH&&i.environment.getIndex()==5)
									break;
							}
							str += arr ? " ]" : "";
							str += "</span>"
						}
					} else if(t=="function"){
						str+="<span class='Function'>function()</span>";
					} else {
						str+="<span class='other'>"+t+"</span>";
					}
					return str;
				}
			},
			historyIndex:0,
			history:[],
			toggleMultiline:function(){
				with(pi.dbgr){
					d.closeActiveNavigation();
					if(env.multilinemode){
						env.multilinemode = false;
						el.consoleOutput.environment.getParent().pi.environment.addStyle({ "height":"150px" });
						el.consoleInput.environment.addStyle({ "height":"13px" });
						el.consoleInput.environment.addStyle({ "overflowY":"hidden" });
						return;	
					}
					env.multilinemode = true;
					el.consoleOutput.environment.getParent().pi.environment.addStyle({ "height":"82px" });
					el.consoleInput.environment.addStyle({ "height":"81px" });
					el.consoleInput.environment.addStyle({ "overflowY":"scroll" });
				}
			},
			print: function(CMD,TEXT){
				with (pi.dbgr){
					d.console.addLine().attribute.addClass("Arrow").update(">>> "+CMD);
					d.console.addLine().update(d.console.format(TEXT));
					d.console.scroll();
					d.console.historyIndex = d.console.history.push(CMD);
				}
			},
			run:function(){
				with(pi.dbgr){
					var cmd = el.consoleInput.environment.getElement().value;
					if(cmd.length==0)return;
					el.consoleInput.environment.getElement().value = "";
	
					try { 
						result = eval(cmd);
						
						d.console.print(cmd,result);
					} catch(e){
						d.console.addLine().attribute.addClass("Arrow").update(">>> "+cmd);
						if(!pi.env.ff){
							d.console.scroll();
							return d.console.addLine().attribute.addClass("Error").update("<strong>Error: </strong>"+e,true);
						}
						if(e.fileName==null){
							d.console.scroll();
							d.console.addLine().attribute.addClass("Error").update("<strong>Error: </strong>"+e.message,true);
						}
						var fileName = e.fileName.split("\/").getLastItem();
						d.console.addLine().attribute.addClass("Error").update("<strong>Error: </strong>"+e.message+" (<em>"+fileName+"</em>,"+e.lineNumber+")",true);
						d.console.scroll();
					}
				
				} 
			},
			scroll:function(){
				with(pi.dbgr){
					el.consoleOutput.environment.getElement().parentNode.scrollTop = Math.abs(el.consoleOutput.environment.getSize().offsetHeight-(env.multilinemode?62:130));
				}
			},
			openNavigation:function(){
				with(pi.dbgr){
					env.activeNavigation = el.consoleNavigation;
					el.consoleNavigation.environment.addStyle({ "display":"block" });
				}
			}
		},
		closeActiveNavigation:function(EVENT){
			with(pi.dbgr){
				var event = EVENT||{ "target":null };
				var target = event.srcElement||event.target;
				if(env.activeNavigation==null)return;
				if(target){
					if(target.parentNode.pi&&target.parentNode.pi.attribute.hasClass("Navigation"))
						return;
				}
				env.activeNavigation.environment.addStyle({
					"display":"none"
				});
				env.activeNavigation = null;
			}
		},
		css:{
			open:function(INDEX){
				with (pi.dbgr) {
					el.cssContent.update("");
					var rules = document.styleSheets[INDEX][pi.env.ie ? "rules" : "cssRules"];
					for (var i = new pi.iterator(rules); !i.atEnd;) {
						var item = i.next();
						var selector = item.selectorText;
						var cssText = pi.env.ie?item.style.cssText:item.cssText.match(/\{(.*)\}/)[1];
						d.css.printRule(selector, cssText.split(";"), el.cssContent);
					}
				}
			},
			printRule:function(SELECTOR,CSS,LAYER){
				with(pi.dbgr){
					LAYER.child.add(new pi.element().attribute.addClass("Selector").environment.addStyle({ "marginTop":"10px" }).update(SELECTOR+" {"));
					for(var i=new pi.iterator(CSS); !i.atEnd;){
						var item = i.next();
						LAYER.child.add(new pi.element().attribute.addClass("CSSText").update(item.replace(/(.+\:)(.+)/,"<span class='CSSProperty'>$1</span><span class='CSSValue'>$2;</span>")));
					}
					LAYER.child.add(new pi.element().attribute.addClass("Selector").update("}"));
				}
			},
			refresh:function(){
				with(pi.dbgr){
					el.cssStylesheetSelectbox.clean();
					for(var i=new pi.iterator(document.styleSheets); !i.atEnd;){
						var item = i.next();
						d.css.index = i.environment.getIndex();
						el.cssStylesheetSelectbox.child.add(
							new pi.element("OPTION").attribute.set("value",i.environment.getIndex()).update(item.href)
						);
					}
					d.css.open(0);
				}
			}
		},
		dom: {
			open:function(OBJECT,NAME_PARENT,VALUE_PARENT){
				with (pi.dbgr) {
					var obj = OBJECT || window;
					var nparent = NAME_PARENT || el.domLeftContent;
					var vparent = VALUE_PARENT || el.domRightContent;
					var step = 0;
					nparent.clean();
					vparent.clean();
					if(nparent.opened&&nparent!=el.domLeftContent){
						nparent.environment.getParent().pi.child.get()[0].pi.attribute.removeClass("Opened");
						nparent.opened = false;
						return;
					}
					nparent.environment.getParent().pi.child.get()[0].pi.attribute.addClass("Opened");
					nparent.opened = true;
					for (var i = new pi.iterator(obj); !i.atEnd;) {
						try {
							var item = i.next();
							var value = item instanceof Array?item[1]:item;
							var property = item instanceof Array?item[0]:i.environment.getIndex();
							""+value;
							
							step = 1;
							var nameContainer = new pi.element("DIV").attribute.addClass("Property").insert(nparent);
							step = 2;
							var valueContainer = new pi.element("DIV").attribute.addClass("Value").update(d.console.format(value)).insert(vparent);
							step = 3;
							var link = new pi.element("A").attribute.addClass(typeof value=="object"&&Boolean(value)?"Object":" ").update(property).insert(nameContainer);
							step = 0;
							
							if(typeof value!="object"||Boolean(value)==false)continue;
							var nameSubContainer = new pi.element("DIV").attribute.addClass("SubContainer").insert(nameContainer);
							var valueSubContainer = new pi.element("DIV").attribute.addClass("SubContainer").insert(valueContainer);
							link.event.addListener("click",d.dom.open.curry(window,item[1],nameSubContainer,valueSubContainer));
						
						} catch(e){
							switch(e){
								case 2:
									nameContainer.remove();
									break;
								case 3:
									nameContainer.remove();
									valueContainer.remove();
									break;
							}
						}
					}
				}
			}
		},
		html:{
			current:null,
			openHtmlTree:function(ELEMENT,PARENT){
				with(pi.dbgr){
					var element = ELEMENT || document.documentElement;
					var parent = PARENT || el.htmlTree;
					
					if(parent!=el.htmlTree){
						var nodeLink = parent.environment.getParent().pi.child.get()[0].pi;
						if(d.html.current)d.html.current[1].attribute.removeClass("Selected");
						nodeLink.attribute.addClass("Selected");
						
						d.html.current = [ELEMENT,nodeLink];
						d.html.openProperties();
					}
					
					if(element.childNodes&&(element.childNodes.length==0||(element.childNodes.length==1&&element.childNodes[0].nodeType==3)))return;
					parent.clean();
					
					if(parent.opened){
						parent.opened = false;
						parent.environment.getParent().pi.child.get()[0].pi.attribute.removeClass("Open");
						return;
					}
					if (parent != el.htmlTree) {
						parent.environment.getParent().pi.child.get()[0].pi.attribute.addClass("Open");
						parent.opened = true;
						
					}
					
					for(var i=new pi.iterator(element.childNodes); !i.atEnd;){
						var item = i.next();
						if(item.nodeType==3)continue;
						var container = new pi.element().attribute.addClass("Block").insert(parent);
						var link = new pi.element("A").attribute.addClass("Link").update(d.console.format(item)).insert(container);
						var subContainer = new pi.element("DIV").attribute.addClass("SubContainer").insert(container);
						link.event.addListener("click",d.html.openHtmlTree.curry(window,item,subContainer));
						
						if(d.html.current==null&&item==document.body){
							link.attribute.addClass("Selected");
							d.html.current = [item,link];
							d.html.openHtmlTree(item,subContainer);
						}
						
						if(item.childNodes&&item.childNodes.length==1&&item.childNodes[0].nodeType==3){
							link.child.get()[0].appendChild(document.createTextNode(item.childNodes[0].nodeValue.substring(0,25)));
							link.child.get()[0].appendChild(document.createTextNode("</"));
							link.child.get()[0].appendChild(new pi.element("span").attribute.addClass("tagName").update(item.nodeName).environment.getElement());
							link.child.get()[0].appendChild(document.createTextNode(">"));
							continue;
						}
						else if(item.childNodes&&item.childNodes.length==0)continue;
						link.attribute.addClass("ParentLink");
						
					}
					
				}
			},
			openProperties:function(){
				with(pi.dbgr){
			
					var index = el.htmlRightNav.environment.getElement().selectedIndex;
					var node = d.html.current[0];
					var property = ["opacity","filter","azimuth","background","backgroundAttachment","backgroundColor","backgroundImage","backgroundPosition","backgroundRepeat","border","borderCollapse","borderColor","borderSpacing","borderStyle","borderTop","borderRight","borderBottom","borderLeft","borderTopColor","borderRightColor","borderBottomColor","borderLeftColor","borderTopStyle","borderRightStyle","borderBottomStyle","borderLeftStyle","borderTopWidth","borderRightWidth","borderBottomWidth","borderLeftWidth","borderWidth","bottom","captionSide","clear","clip","color","content","counterIncrement","counterReset","cue","cueAfter","cueBefore","cursor","direction","display","elevation","emptyCells","cssFloat","font","fontFamily","fontSize","fontSizeAdjust","fontStretch","fontStyle","fontVariant","fontWeight","height","left","letterSpacing","lineHeight","listStyle","listStyleImage","listStylePosition","listStyleType","margin","marginTop","marginRight","marginBottom","marginLeft","markerOffset","marks","maxHeight","maxWidth","minHeight","minWidth","orphans","outline","outlineColor","outlineStyle","outlineWidth","overflow","padding","paddingTop","paddingRight","paddingBottom","paddingLeft","page","pageBreakAfter","pageBreakBefore","pageBreakInside","pause","pauseAfter","pauseBefore","pitch","pitchRange","playDuring","position","quotes","richness","right","size","speak","speakHeader","speakNumeral","speakPunctuation","speechRate","stress","tableLayout","textAlign","textDecoration","textIndent","textShadow","textTransform","top","unicodeBidi","verticalAlign","visibility","voiceFamily","volume","whiteSpace","widows","width","wordSpacing","zIndex"].sort();
					el.htmlRightContent.clean();
					var str = "<div class='Item'><div class='Property'><center>Property</center></div><div class='Value'><center>Value</center></div></div>";
					switch(index){
						case 0:
							var view = document.defaultView?document.defaultView.getComputedStyle(node,null):node.currentStyle;
							for(var i=new pi.iterator(property); !i.atEnd;){
								var item = i.next();
								if(!view[item])continue;
								str+="<div class='Item'><div class='Property'>"+item+"</div><div class='Value'>"+d.console.format(view[item])+"</div></div>";
							}
							break;
						case 1:

							for(var i=new pi.iterator(node,2); !i.atEnd;){
								var item = i.next();
								try {
									if(!Boolean(item[1]))continue;
									else if(typeof item[1]=="string"){
										item[1] = item[1].substring(0,70).replace(/</g,"&#60;").replace(/>/g,"&#62;")+" ..."
									}
									str+="<div class='Item'><div class='Property'>"+item[0]+"</div><div class='Value'>"+d.console.format(item[1],true)+"</div></div>";
								}catch(e){ continue; }
							}
							break;
					}
					el.htmlRightContent.environment.getElement().innerHTML=str;
				}
			}
		},
		navigate:function(INDEX){
			with(pi.dbgr){
				el.content[env.dNames[env.dIndex]].environment.addStyle({ "display":"none" });
				el.content[env.dNames[INDEX]].environment.addStyle({ "display":"block" });
				el[env.dNames[env.dIndex]+"TabButton"].environment.getParent().pi.attribute.removeClass("Selected");
				el[env.dNames[INDEX]+"TabButton"].environment.getParent().pi.attribute.addClass("Selected");
				env.dIndex = INDEX;
				
				switch(INDEX){
					case 1:
						d.html.openHtmlTree();
						break;
					case 2:
						d.css.refresh();
						break;
					case 3:
						d.dom.open();
						break;
				}
			}
		},
		refreshSize:function(){
			with(pi.dbgr){
				el.main.environment.addStyle({
					"width":parseInt(pi.env.getWindowSize().width)-20+"px"
				});
				el.content.console.environment.addStyle({
					"width":parseInt(pi.env.getWindowSize().width)-40+"px"
				});
				el.queryInput.environment.addStyle({
					"width":parseInt(pi.env.getWindowSize().width)-55+"px"
				});
				el.consoleInput.environment.addStyle({
					"width":parseInt(pi.env.getWindowSize().width)-100+"px"
				});
			}
		}
	},
	listen: {
		queryInput:function(EVENT){
			with(pi.dbgr){
				if(EVENT.keyCode==13)
					return d.console.run();
		
				el.consoleInput.update("pi.xpath('{0}','ORDERED_NODE_SNAPSHOT_TYPE',document,null,null)".format(el.queryInput.environment.getElement().value))
			}
		},
		consoleInput:function(EVENT){
			with(pi.dbgr){
				if(EVENT.keyCode==13&&(env.multilinemode==false||EVENT.shiftKey==false)){
					d.console.historyIndex = d.console.history.length;
					d.console.run();
					return false;
				}
				
				if(env.multilinemode||[13,38,40].indexOf(EVENT.keyCode)==-1)return;
				d.console.historyIndex+=EVENT.keyCode!=40?0:d.console.historyIndex==d.console.history.length?0:1;
				d.console.historyIndex-=EVENT.keyCode!=38?0:d.console.historyIndex==0?0:1;
				el.consoleInput.update(
					d.console.history.length > d.console.historyIndex ?
					d.console.history[d.console.historyIndex] :
					""
				);
				
			}
		},
		domInputbox:function(EVENT){
			with(pi.dbgr){
				if(EVENT.keyCode==13){
					var cmd = el.domInputbox.environment.getElement().value;
					d.console.dir(eval(cmd));
				}
			}
		},
		htmlNav:function(){
			with(pi.dbgr){
				d.html.openProperties();
			}
		},
		stylesheetSelectbox:function(){
			with(pi.dbgr){
				d.css.open(el.cssStylesheetSelectbox.environment.getElement().selectedIndex);
			}
		}
	}
};
pi.env.addEvent(window,"resize",pi.dbgr.d.refreshSize);
pi.env.addEvent(document,"mousedown",pi.dbgr.d.closeActiveNavigation);
pi.env.DOMContentLoaded.init = pi.dbgr.init;

if(!window.console){
	window.console = pi.dbgr.d.console;
}
